﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Newtonsoft.Json;

namespace Gw2Api
{
    public struct ObjectiveUpgradeDetails
    {
        public int CurrentTierId;
        public int CurrentTierYaks;
        public WvWUpgrade.WvWUpgradeTier NextTier;
    }

    public class WvWUpgrade
    {
        public int id;

        public class WvWUpgradeTier
        {
            public string name;
            public int yaks_required;

            public class WvWUpgradeTierUpgrade
            {
                public string name;
                public string description;
                public string icon;
            }

            public List<WvWUpgradeTierUpgrade> upgrades;
        }

        public List<WvWUpgradeTier> tiers;

        public ObjectiveUpgradeDetails GetObjectiveUpgradeDetails(int yaks)
        {
            int tierId = 0;
            int tierYaks = yaks;
            foreach (var tier in tiers)
            {
                if (tierYaks >= tier.yaks_required)
                {
                    tierId++;
                    tierYaks = tierYaks - tier.yaks_required;
                }
                else
                {
                    break;
                }
            }
            ObjectiveUpgradeDetails details = new ObjectiveUpgradeDetails
            {
                CurrentTierId = tierId,
                CurrentTierYaks = tierYaks,
                NextTier = tiers.Count > tierId ? tiers[tierId] : null
            };
            return details;
        }

    }

    public class UpgradeApi : BaseApi
    {
        public List<WvWUpgrade> upgrades;

        public UpgradeApi()
        {
            string s = LoadFromUrl(ApiUrls.V2.Upgrades);
            upgrades = JsonConvert.DeserializeObject<List<WvWUpgrade>>(s);
        }
    }
}
